package com.test.demo.javabasis.date;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.*;
import java.util.Locale;

public class DateDemo {

    public static final DateTimeFormatter DATE_TIME_FORMATTER;
    static {
        DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    }

    public static void localTimeandlockDateDemo(){
        System.out.println("==============================LocalDate=========================");
        LocalDate now = LocalDate.now();
        now=LocalDate.of(2021,1,28);
        now=LocalDate.parse("2021-01-28");
        System.out.println(now.getYear());
        System.out.println(now.getMonth());
        System.out.println(now.getMonth().getValue());
        System.out.println(now.getDayOfMonth());
        System.out.println(now.getDayOfWeek());
        System.out.println(now.getDayOfWeek().getValue());
        System.out.println(now.getDayOfYear());
        System.out.println(now.isLeapYear());
        System.out.println(now.lengthOfMonth());

        System.out.println(now.get(ChronoField.YEAR));
        System.out.println(now.get(ChronoField.DAY_OF_MONTH));
        System.out.println(now.get(ChronoField.DAY_OF_WEEK));
        System.out.println(now.get(ChronoField.MONTH_OF_YEAR));

        System.out.println("==============================LocalTime=========================");
        LocalTime time = LocalTime.of(10, 49, 50);
        time=LocalTime.parse("10:49:50");
        System.out.println(time.getHour());
        System.out.println(time.getMinute());
        System.out.println(time.getSecond());

        System.out.println("==============================LocalDateTime=========================");
        LocalDateTime dateTime1 = LocalDateTime.of(2021, Month.JANUARY, 28, 10, 49, 50);
        LocalDateTime dateTime2 = LocalDateTime.of(now, time);
        LocalDateTime dateTime3 = now.atTime(10, 49, 50);
        LocalDateTime dateTime = now.atTime(time);
        LocalDateTime dateTime4 = time.atDate(now);
        LocalDate localDate = dateTime.toLocalDate();
        LocalTime localTime = dateTime.toLocalTime();

    }

    public static void instantDemo() {
        System.out.println("==============================Instant=========================");
        Instant instant = Instant.ofEpochSecond(3);
        Instant.ofEpochSecond(3,0);
        Instant.ofEpochSecond(2,1_000_000_000);
        Instant.ofEpochSecond(4,-1_000_000_000);
        instant=Instant.now();
        System.out.println(instant.getEpochSecond());
    }

    public static void durationAndPeriodDemo() {
        System.out.println("==============================Duration=========================");
        LocalTime localTime = LocalTime.of(10, 10, 10);
        LocalTime localTime1 = LocalTime.of(10, 50, 10);
        Duration duration = Duration.between(localTime, localTime1);
        System.out.println(duration.getSeconds());
        LocalDate now = LocalDate.now();
        LocalDateTime dateTime = localTime.atDate(now);
        LocalDateTime dateTime1 = localTime1.atDate(now);
        Duration duration2 = Duration.between(dateTime, dateTime1);
        System.out.println(duration2.getSeconds());
        Instant instant = Instant.now();
        Instant instant2 = Instant.now();
        Duration duration3 = Duration.between(instant, instant2);
        System.out.println(duration3.getSeconds());

        System.out.println("==============================Period=========================");
        Period period = Period.between(LocalDate.of(2021, 2, 10), LocalDate.now());
        System.out.println(period.getDays());

        Duration duration1 = Duration.of(3, ChronoUnit.MINUTES);
        Duration duration4 = Duration.ofMinutes(3);

        Period period1 = Period.of(20, 1, 5);
        Period period2 = Period.ofDays(20);
        Period period3 = Period.ofYears(3);

        Period period4 = Period.from(period1);
    }

    /**
     * 对localDate进行操作但是不改变原对象
     */
    public static void operateDate(){
        System.out.println("==============================operateDate=========================");
        LocalDate localDate = LocalDate.now();
        LocalDate localDate1 = localDate.withMonth(3);
        LocalDate localDate2 = localDate.withYear(2020);
        LocalDate localDate3 = localDate.with(ChronoField.DAY_OF_MONTH, 30);


        LocalDate localDate4 = localDate.plusDays(3);
        LocalDate localDate5 = localDate.minusWeeks(1);
        LocalDate localDate6 = localDate.plus(2, ChronoUnit.MONTHS);
        System.out.println(localDate.toString()+localDate1.toString()+localDate2.toString()+localDate3.toString()+localDate4.toString()+localDate5.toString()+localDate6.toString()+localDate.toString());
    }

    /**
     * 一些高级筛选时间的方法都在TemporalAdjusters静态方法中
     */
    public static void temporalAdjusterDemo(){
        LocalDate localDate = LocalDate.now();
        //下一个周几（周一）
        LocalDate localDate1 = localDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
        //这个月最后一天
        LocalDate localDate2 = localDate.with(TemporalAdjusters.lastDayOfMonth());
        //从当月算起的第几个周几（周一）
        LocalDate localDate3 = localDate.with(TemporalAdjusters.dayOfWeekInMonth(6, DayOfWeek.MONDAY));
        //倒数
        LocalDate localDate4 = localDate.with(TemporalAdjusters.dayOfWeekInMonth(-2, DayOfWeek.MONDAY));

        LocalDate localDate5 = localDate.with(TemporalAdjusters.firstDayOfYear());
        System.out.println(localDate.toString()+":"+localDate1.toString()+":"+localDate2.toString()+":"+localDate3.toString()+":"+localDate4.toString()+":"+localDate5.toString());
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime localDateTime1 = localDateTime.with(TemporalAdjusters.firstDayOfYear());
        LocalDateTime localDateTime2 = localDateTime.with(TemporalAdjusters.lastDayOfYear());

        LocalDateTime localDateTime3 = LocalDateTime.of(localDate5, LocalTime.MAX);
        System.out.println(localDateTime1.toString()+":"+localDateTime2.toString()+":"+localDateTime3.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        System.out.println(localDateTime3.format(DATE_TIME_FORMATTER));
    }

    /**
     * 打印日期和解析日期，相对老的DateFormat相比DateTimeFormatter是线程安全的，可以单例创建格式器
     */
    public static void dateTimeFormaterDemo(){
        LocalDate localDate = LocalDate.now();
        String format = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
        String format1 = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
        System.out.println(format+"-----"+format1);

        LocalDate localDate1 = LocalDate.parse(format, DateTimeFormatter.BASIC_ISO_DATE);
        LocalDate localDate2 = LocalDate.parse(format1, DateTimeFormatter.ISO_LOCAL_DATE);
        System.out.println(localDate1.toString()+":"+localDate2.toString());

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        String format2 = localDate.format(dateTimeFormatter);
        LocalDate parse = LocalDate.parse(format2, dateTimeFormatter);
        System.out.println(format2+"====="+parse.toString());

        //DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("d. MMMM yyyy", Locale.ITALIAN);
        DateTimeFormatter dateTimeFormatter1 = new DateTimeFormatterBuilder()
                .appendText(ChronoField.DAY_OF_MONTH)
                .appendLiteral(". ")
                .appendText(ChronoField.MONTH_OF_YEAR)
                .appendLiteral(" ")
                .appendText(ChronoField.YEAR)
                .parseCaseInsensitive()
                .toFormatter(Locale.CHINESE);//指定语言
        String format3 = localDate.format(dateTimeFormatter1);
        LocalDate parse2 = LocalDate.parse(format3, dateTimeFormatter1);
        System.out.println(format3+"====="+parse2.toString());


    }

    /**
     * 时区操作
     */
    public static void zoneIdDemo(){
        ZoneId zoneId = ZoneId.of("Europe/Rome");

        LocalDate localDate = LocalDate.now();
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(zoneId);

        LocalDateTime localDateTime = LocalDateTime.now();
        ZonedDateTime zonedDateTime1 = localDateTime.atZone(zoneId);

        Instant instant = Instant.now();
        ZonedDateTime zonedDateTime2 = instant.atZone(zoneId);

        LocalDateTime localDateTime1 = localDateTime.ofInstant(instant, zoneId);
        System.out.println(zonedDateTime.toString()+":"+zonedDateTime1.toString()+":"+zonedDateTime2.toString()+":"+localDateTime1.toString());
    }

    public static void main(String[] args) {
        //适用于人看
        //localTimeandlockDateDemo();
        //适用于机器
        //instantDemo();
        //durationAndPeriodDemo();
        //operateDate();
        //temporalAdjusterDemo();
        //dateTimeFormaterDemo();
        //zoneIdDemo();
    }
}
